package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.UserDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.mapper.UserMapper;
import com.carkeeper.pojo.StoreUserPO;
import com.carkeeper.service.UserService;
import com.carkeeper.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author ：XXX
 * @date ：Created in 2020/5/27
 * @description ：
 * @version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public UserVO queryUserById(Long id) {
        StoreUserPO userPojo = userMapper.findUserById(id);
        if (userPojo == null) {
            return null;
        }
        UserVO userVo = new UserVO();
        BeanUtils.copyProperties(userPojo, userVo);
        return userVo;
    }

    /**
     * 更新用户
     *
     * @param userPojo
     */
    @Override
    public boolean add(StoreUserPO userPojo) {
        return userMapper.insert(userPojo) > 0;
    }


    /**
     * 修改用户
     *
     * @param userPojo
     */
    @Override
    public boolean update(StoreUserPO userPojo) {
        return userMapper.updateById(userPojo) > 0;
    }

    /**
     * 删除用户
     *
     * @param userId
     */
    @Override
    public boolean delete(long userId) {
        return userMapper.deleteById(userId) > 0;

    }

    /**
     * 获取分页数据
     *
     * @param username
     * @param realname
     * @param phone
     * @param storeId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<StoreUserPO> findAll(long page, long pageSize, String username, String realname, String phone, Long storeId) {
        Page<StoreUserPO> pageResult = new Page<>(page, pageSize);
        LambdaQueryWrapper<StoreUserPO> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(username)) {
            wrapper.like(StoreUserPO::getUsername, username);
        }
        if (!StringUtils.isEmpty(realname)) {
            wrapper.like(StoreUserPO::getRealname, realname);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.like(StoreUserPO::getPhone, phone);
        }
        if (!StringUtils.isEmpty(storeId)) {
            wrapper.eq(StoreUserPO::getStoreId, storeId);
        }


        return userMapper.selectPage(pageResult, wrapper);
    }

    @Override
    public List<UserDTO> getByIds(List<Long> ids) {
        List<UserDTO> dtoList = new ArrayList<>();
        List<StoreUserPO> userPojos = userMapper.selectBatchIds(ids);
        for (StoreUserPO userPojo : userPojos) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userPojo, userDTO);
            dtoList.add(userDTO);
        }
        return dtoList;
    }

    /**
     * 校验账号密码
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public StoreUserPO login(String username, String password) {
        StoreUserPO em = new StoreUserPO();
        em.setUsername(username);

        QueryWrapper<StoreUserPO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StoreUserPO::getUsername, username);
        //从数据库查询出的对象
        StoreUserPO storeUserPO = userMapper.selectOne(wrapper);
        if (storeUserPO == null) {
            //如果数据库中查询到的用户对象为空, 则返回登录失败
            throw new BussinessException("用户不存在!!!");
        }
        if (storeUserPO.getPassword().equals(password)) {
            return storeUserPO;
        }
        throw new BussinessException("密码错误!");
    }

    @Override
    public StoreUserPO findByUsername(String username) {
        QueryWrapper<StoreUserPO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StoreUserPO::getUsername, username);
        //从数据库查询出的对象
        StoreUserPO storeUserPO = userMapper.selectOne(wrapper);
        if (storeUserPO == null) {
            throw new BussinessException("查找的用户不存在！");
        }
        return storeUserPO;
    }

}
